//给vue添加一个全局属性
Vue.prototype.$http=axios;
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8082/'

//1 使用axios前置拦截器，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
    //携带token
    let uToken =  localStorage.getItem("uToken");
    if(uToken){
        config.headers['U-TOKEN']=uToken;
    }
    return config;
},error => {
    Promise.reject(error);
})
//2 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && data.message==="noUser")
        location.href = "/login.html";
    return result;
},error => {
    Promise.reject(error);
})

/**拆解url从中获取 code和state 并封装为对象*/
function parseUrlParams2Obj(url){
    var obj = {};
    //http://bugtracker.itsource.cn/callback.html?code=061JN7Jd0Mpx1w1oNDJd0rTaJd0JN7Jj&state=1
    //var url = location.href;
    url =  url.substr(url.indexOf("?")+1)//indexOf 得到？ 的下标  substr返回从下标开始的字段
    //console.log(url);//code=02195sM12cYfDS05bpO12lihM1295sMN&state=1
    if(url){
        var urls = url.split("&");//通过&分割为字符串数组
        //console.log(urls); // "code=02195sM12cYfDS05bpO12lihM1295sMN" "state=1"
        //遍历赋值
        for(var i = 0;i<urls.length;i++){
            var kvs = urls[i]; // code=061JN7Jd0Mpx1w1oNDJd0rTaJd0JN7Jj state=1
            //console.log(kvs);
            var key = kvs.split("=")[0]//通过等号分割 得到code和state作为key
            var value = kvs.split("=")[1];//通过等号分割 得到code的值和state的值作为value
            obj[key] = value; // 把key的值作为动态熟悉添加值
        }
    }
    //console.log(obj)//Object { code: "02195sM12cYfDS05bpO12lihM1295sMN", state: "1" }
    return obj;
}


